1 00:00:00,500 --> 00:00:01,750 Thanks for joining Mac. 2 00:00:01,790 --> 00:00:02,480 In this lecture. 3 00:00:02,480 --> 00:00:05,930 We're going to take a look at how to create a simple crafting system. 4 00:00:05,930 --> 00:00:09,260 Before we get started, though, we'll have to do a little bit of setup. 5 00:00:09,650 --> 00:00:15,890 I've attached this guy to the lecture that you'll need to follow along, and I've also attached some 6 00:00:15,920 --> 00:00:20,660 tool models that I've placed here inside of a folder called Craftable Items. 7 00:00:20,660 --> 00:00:26,840 Just some random tools I made that will be using as an example for our crafting system. 8 00:00:26,930 --> 00:00:30,110 So you'll want to go ahead and grab these before we get started. 9 00:00:30,810 --> 00:00:35,460 Another thing we must do is that I've created a remote function and replicated storage. 10 00:00:35,460 --> 00:00:40,020 I've just called it request craft info, and we're going to be using this to communicate between the 11 00:00:40,020 --> 00:00:42,480 client and server when they need to craft something. 12 00:00:42,630 --> 00:00:47,370 But once we've got all of this finished, then we can go ahead and get started into our scripting. 13 00:00:47,640 --> 00:00:54,120 So inside of server script service, I'm going to create a server script and I'll just name it crafting 14 00:00:54,120 --> 00:00:55,290 System handler. 15 00:00:55,290 --> 00:01:00,150 And this is going to handle all of the crafting and server validation stuff. 16 00:01:00,270 --> 00:01:03,020 So the first thing we need are some services. 17 00:01:03,030 --> 00:01:10,650 We're going to need some variables as well as functions and then some event handlers. 18 00:01:13,090 --> 00:01:19,090 So the services we're going to be needing for our service script is that first we need the player service. 19 00:01:20,780 --> 00:01:26,690 We're also going to need to get access to replicated storage so we can reference that event in there. 20 00:01:28,650 --> 00:01:33,060 And then we'll also need a reference to server storage so we can grab those tools and give them to our 21 00:01:33,060 --> 00:01:34,860 player when they want to craft something. 22 00:01:39,060 --> 00:01:41,100 So let's go ahead and reference that event. 23 00:01:41,100 --> 00:01:45,930 I'll just call it craft event, and it's going to be equal to replicated storage and get a reference 24 00:01:45,930 --> 00:01:49,530 to our request craft info, remote function. 25 00:01:50,130 --> 00:01:55,200 And then I'm also going to reference the folder that houses all of our tools. 26 00:01:55,200 --> 00:01:58,290 So server storage dot craftable items. 27 00:01:59,100 --> 00:02:01,890 The next thing I'm going to do is I'm going to create a table. 28 00:02:01,890 --> 00:02:03,990 I'll just call it craftable items. 29 00:02:03,990 --> 00:02:10,170 And this table is going to store the information of what materials we exactly need for each of the items. 30 00:02:10,170 --> 00:02:13,650 So one of our tools is a wooden pickaxe. 31 00:02:13,650 --> 00:02:17,700 We want to make sure that this key is the exact same as the name of our tool. 32 00:02:17,880 --> 00:02:23,790 And in here, I'm just going to store a table and this table is going to have a key value pair. 33 00:02:24,000 --> 00:02:28,800 One of the materials I'll have is wood and I'll set that equal to five. 34 00:02:28,800 --> 00:02:31,830 So for our wooden pickaxe, we need five wood. 35 00:02:32,010 --> 00:02:35,160 Then we could do for our stone pickaxe. 36 00:02:35,920 --> 00:02:37,270 We'll need. 37 00:02:37,300 --> 00:02:41,860 Let's say we'll need two wood, and then we'll also need three stone. 38 00:02:42,730 --> 00:02:45,700 And then we could do the same for our iron pickaxe. 39 00:02:46,690 --> 00:02:47,080 Again. 40 00:02:47,080 --> 00:02:51,280 We'll need to wood and then we'll need three iron. 41 00:02:53,240 --> 00:02:56,000 And then the last thing, we'll have a crystal pickaxe. 42 00:02:57,610 --> 00:03:02,830 And again, we'll need to wood and then we'll have three crystal. 43 00:03:03,970 --> 00:03:05,550 I'm going to create one more table. 44 00:03:05,560 --> 00:03:07,840 I'm going to call it craft bounce. 45 00:03:07,840 --> 00:03:13,810 And what this table is going to do is it's going to store the names of the players who fire this remote 46 00:03:13,810 --> 00:03:14,620 function. 47 00:03:14,620 --> 00:03:17,650 And I'm going to keep track of them that way. 48 00:03:17,650 --> 00:03:22,030 You know, they can't sit there and spam the remote function a bunch of times. 49 00:03:22,180 --> 00:03:26,650 Now, one function we're going to need is for our remote function. 50 00:03:26,650 --> 00:03:30,460 So I'm going to call this function on craft event. 51 00:03:30,460 --> 00:03:37,810 So whenever this function gets called from the client to the server, it's first going to pass to us 52 00:03:37,810 --> 00:03:42,330 a player, the player who called this remote function from the client. 53 00:03:42,340 --> 00:03:47,260 I guess we'll pass in action what we want the client to do for us and that's going to be a string and 54 00:03:47,260 --> 00:03:53,350 then we'll pass another string and that's going to be the name of the item that the player wants to 55 00:03:53,350 --> 00:03:54,250 craft. 56 00:03:55,200 --> 00:03:56,790 So we'll have that function. 57 00:03:57,120 --> 00:03:59,610 And then we're also going to need another function. 58 00:03:59,610 --> 00:04:02,880 And this function I'll just call it create int value. 59 00:04:04,260 --> 00:04:09,450 And with this function is going to do for us is that when a player joins our game, we're going to create 60 00:04:09,450 --> 00:04:15,000 a folder that's a child of the player and that folder is going to store all of the materials that player 61 00:04:15,000 --> 00:04:21,600 has and it's going to be stored in an int value so that int value will have a name, it'll have a value, 62 00:04:21,600 --> 00:04:24,090 and then we'll set the parent for that int value. 63 00:04:24,090 --> 00:04:27,930 So we'll actually do that real quick in this function we'll just create a new int value. 64 00:04:30,880 --> 00:04:32,680 The int value. 65 00:04:32,720 --> 00:04:37,000 Dot name is going to be equal to the name that is passed to the function. 66 00:04:37,240 --> 00:04:39,640 Same thing for the value. 67 00:04:40,720 --> 00:04:43,720 And then the same thing for the parent. 68 00:04:44,920 --> 00:04:46,780 And that's all we need this function to do. 69 00:04:47,730 --> 00:04:54,150 Before we fill out this function, we should create our event handlers and one is going to be for the 70 00:04:54,150 --> 00:04:59,010 player added event and the player service will connect a function to that and get the player that is 71 00:04:59,010 --> 00:04:59,610 added. 72 00:05:00,500 --> 00:05:04,040 We'll also need to listen to when our remote function gets called. 73 00:05:04,040 --> 00:05:05,900 So craft event. 74 00:05:06,230 --> 00:05:11,540 On server is invoked, it's going to be equal to that on craft event function. 75 00:05:11,870 --> 00:05:17,840 So the on server invoke property is referencing our function and this function is going to execute and 76 00:05:17,840 --> 00:05:20,300 do the functionality we want the server to do. 77 00:05:21,080 --> 00:05:27,050 One more event I think I need to listen to is when a player gets removed from our game. 78 00:05:27,860 --> 00:05:33,950 And the reason we need to do this is because I'm going to be storing player names in here and it's going 79 00:05:33,950 --> 00:05:39,350 to be equal to a Boolean to see whether or not they've already fired this remote event. 80 00:05:39,350 --> 00:05:44,630 But if the player leaves the game, then I need to clear their name out of this table so we don't keep 81 00:05:44,630 --> 00:05:50,600 sucking up names inside of this table and storing memory that we don't need to because let's say the 82 00:05:50,600 --> 00:05:54,980 lifetime of the server is around for a long time and it just keeps storing more and more and more player 83 00:05:54,980 --> 00:05:55,220 names. 84 00:05:55,220 --> 00:05:58,670 It's going to take up memory that can be freed later. 85 00:05:58,940 --> 00:06:06,620 So actually what we could do is we could check if that if the craft debounce table has a key value pair 86 00:06:06,620 --> 00:06:07,760 for this player. 87 00:06:08,780 --> 00:06:15,200 Then what we could do is we could just set it to nil, which allows us to free up the memory. 88 00:06:16,420 --> 00:06:20,740 Now, when a player joins our game, we want to create a new folder for them. 89 00:06:20,740 --> 00:06:22,900 We'll call it material folder. 90 00:06:25,780 --> 00:06:26,320 Older. 91 00:06:26,890 --> 00:06:31,540 The name of our material folder is going to be materials. 92 00:06:33,370 --> 00:06:38,380 And then the material folder parent is going to be equal to that player instance. 93 00:06:40,510 --> 00:06:40,840 Next. 94 00:06:40,840 --> 00:06:45,670 What we could do is we could call that create value function and create all of those materials a player 95 00:06:45,670 --> 00:06:46,340 should have. 96 00:06:46,360 --> 00:06:51,430 So one material is going to be wood, and when they join the game, we'll just give them a default of 97 00:06:51,430 --> 00:06:52,060 zero. 98 00:06:52,060 --> 00:06:55,870 And the parent for that is going to be that material folder we just created. 99 00:06:55,960 --> 00:07:01,420 And then we can actually just copy this and paste this function four more times and then we can do one 100 00:07:01,420 --> 00:07:02,380 for stone. 101 00:07:02,380 --> 00:07:03,820 We can do one. 102 00:07:05,800 --> 00:07:06,730 For iron. 103 00:07:07,240 --> 00:07:09,460 And then we can do one for crystal. 104 00:07:11,120 --> 00:07:12,880 And I think that's all we need to do there. 105 00:07:12,890 --> 00:07:17,780 So when a player joins our game, we create a bunch of new INT values in a folder for the player, and 106 00:07:17,780 --> 00:07:20,900 that folder belongs to that specific player instance. 107 00:07:21,820 --> 00:07:27,550 So whenever the player is in our game, they'll have this on our screen and they'll be able to open 108 00:07:27,550 --> 00:07:29,020 the GUI with these buttons. 109 00:07:29,020 --> 00:07:34,750 So if I actually go to the crafting UI here, one of the frames show us the items we can craft. 110 00:07:34,750 --> 00:07:41,140 And my idea is to tween the position to show on the screen when they press that button and it'll show 111 00:07:41,140 --> 00:07:46,690 us the items that they can craft, and then there'll be another side that will show them what materials 112 00:07:46,690 --> 00:07:47,320 they have. 113 00:07:47,320 --> 00:07:51,940 So it'll show them the materials and it'll show them the quantity of the materials. 114 00:07:51,940 --> 00:07:58,300 And to do that, I actually have some frames in here and this is just going to be an example. 115 00:07:58,300 --> 00:08:05,050 But if I put it into the scrolling frame and then I actually enable the visibility, it'll display the 116 00:08:05,050 --> 00:08:08,830 name of the material and then it'll display the quantity of the material. 117 00:08:10,050 --> 00:08:11,910 Let me just put that back real quick. 118 00:08:12,960 --> 00:08:17,610 And then for our item frame, we have a button actually, because the player will click that button. 119 00:08:17,610 --> 00:08:20,670 If they want to craft it, let's put it in the scrolling flame. 120 00:08:22,310 --> 00:08:28,070 It'll show them the name of the item and it'll also list out the requirements or what exactly materials 121 00:08:28,070 --> 00:08:30,380 they'll need to craft that item. 122 00:08:31,850 --> 00:08:35,810 So let me move these frames back to their default positions. 123 00:08:40,280 --> 00:08:42,600 And we can fill out the rest of our server script. 124 00:08:42,620 --> 00:08:48,170 So what do we want to happen when a player presses one of those buttons and they want to craft an item? 125 00:08:48,960 --> 00:08:54,720 Well, the first thing we need to do is to check if the string that the player passes to the server, 126 00:08:54,750 --> 00:08:59,520 the action is, let's say the action is equal to craft item. 127 00:08:59,520 --> 00:09:01,170 So they want to craft an item, right? 128 00:09:01,950 --> 00:09:06,660 So if they want to craft an item, then the first thing we need to do is we need to check if they have 129 00:09:06,690 --> 00:09:07,710 that debounce. 130 00:09:07,710 --> 00:09:12,060 So if they have a key value pair in the table, right? 131 00:09:12,060 --> 00:09:14,910 And it's true, then we're going to return. 132 00:09:15,030 --> 00:09:20,340 And because this is a remote function, we actually have to return something back. 133 00:09:20,340 --> 00:09:22,200 I'm just going to return false. 134 00:09:22,200 --> 00:09:29,160 As you know, they can't craft the item because they're currently in a debounce and then after that 135 00:09:29,160 --> 00:09:30,330 we can actually. 136 00:09:31,040 --> 00:09:34,340 Set a key value pair for them to true. 137 00:09:34,370 --> 00:09:39,320 That way they can't you know spam this remote function because exploiters could do that. 138 00:09:40,310 --> 00:09:47,060 Once we verify that this is their first calling of this function, we need to create some references 139 00:09:47,060 --> 00:09:49,880 to the folder that is in the player. 140 00:09:49,880 --> 00:09:51,950 So player materials. 141 00:09:53,290 --> 00:09:57,910 We'll get there materials folder that we create when the player first joins the game. 142 00:09:59,610 --> 00:10:04,530 We're also going to create a variable that's going to keep track of whether or not this item that they're 143 00:10:04,530 --> 00:10:06,730 requesting to craft can be crafted or not. 144 00:10:06,750 --> 00:10:09,000 And by default, I'm going to set it to false. 145 00:10:09,420 --> 00:10:11,730 And then we'll also need a table. 146 00:10:12,240 --> 00:10:13,860 I'll call it materials. 147 00:10:14,620 --> 00:10:18,310 Materials to be deducted. 148 00:10:18,820 --> 00:10:25,150 Set it to a table, because what we're going to do is we're going to loop through all of the materials 149 00:10:25,150 --> 00:10:30,730 in the player and see if they have enough of the requirements that are defined up here to be able to 150 00:10:30,730 --> 00:10:31,720 craft the item. 151 00:10:31,720 --> 00:10:36,040 And if they do, then we'll put all those materials they need to deduct in this table and we'll deduct 152 00:10:36,040 --> 00:10:39,340 it from those values that are in their materials folder. 153 00:10:40,360 --> 00:10:47,830 Now, one thing we need to check is whether or not the string this name, they want to craft the item. 154 00:10:47,830 --> 00:10:48,160 Right. 155 00:10:48,160 --> 00:10:50,710 If it exists in our craftable items table. 156 00:10:50,710 --> 00:10:59,290 So what we could do is that if this item the requesting the craft does not exist within our craftable 157 00:10:59,290 --> 00:11:06,520 items table, then we can set their craft bounce back to false. 158 00:11:07,850 --> 00:11:08,620 Or actually, you know what? 159 00:11:08,630 --> 00:11:09,590 We don't need to do that. 160 00:11:09,590 --> 00:11:11,780 We'll just return. 161 00:11:12,760 --> 00:11:13,960 False. 162 00:11:14,820 --> 00:11:18,960 And we actually put this as one of the first things we can check here. 163 00:11:20,660 --> 00:11:26,090 So if the item that they're requesting to craft, if it doesn't exist, then we're going to return. 164 00:11:28,000 --> 00:11:33,460 Once we get this finished, we're going to loop through every single material and the amount that is 165 00:11:33,460 --> 00:11:36,040 required for the specific item they want to craft. 166 00:11:36,040 --> 00:11:40,570 So, for example, if they pass a string of wooden pickaxe, we're going to loop through every single 167 00:11:40,600 --> 00:11:42,370 key value pair in this table. 168 00:11:42,370 --> 00:11:43,900 For this one, it's just one. 169 00:11:43,900 --> 00:11:47,890 So we'll get the material, which is wood, and then the amount is five. 170 00:11:48,040 --> 00:11:55,660 So for every material and amount in pairs, we're going to get the craftable items table and we're going 171 00:11:55,660 --> 00:11:57,670 to get the item name. 172 00:11:57,670 --> 00:11:59,470 So this particular. 173 00:12:00,230 --> 00:12:01,370 Key value pair. 174 00:12:03,520 --> 00:12:12,190 What we're going to do is we'll check if if the players materials folder if it doesn't have. 175 00:12:13,210 --> 00:12:16,060 Fine first child this material. 176 00:12:17,710 --> 00:12:18,820 Or. 177 00:12:19,500 --> 00:12:21,450 We could say oops. 178 00:12:21,450 --> 00:12:22,640 Materials. 179 00:12:22,650 --> 00:12:24,030 Materials. 180 00:12:25,970 --> 00:12:33,020 Or let's say the player materials folder of this player if they do have this material. 181 00:12:33,020 --> 00:12:41,000 But the amount of materials they have, like let's say they only have one wood and they need five wood 182 00:12:41,000 --> 00:12:46,370 to craft the wooden pickaxe, Obviously we can't craft it, so we'll have to return false. 183 00:12:46,370 --> 00:12:51,650 So if it's less than the amount that is required for this specific item. 184 00:12:52,390 --> 00:12:54,640 Then what we could do is we could. 185 00:12:55,220 --> 00:13:02,030 Set can be crafted actually to false, because what I'm going to do is actually set this to true. 186 00:13:02,030 --> 00:13:07,820 So we're going to assume that the item they want to craft can be crafted until proven otherwise. 187 00:13:07,820 --> 00:13:14,390 So when we loop through every single material and amount they need for this particular item, if they 188 00:13:14,390 --> 00:13:20,660 don't have this material in their player materials folder or they don't have enough quantity of this 189 00:13:20,660 --> 00:13:26,480 material, then we're going to set can be crafted to false and we're going to break out of this loop. 190 00:13:27,780 --> 00:13:34,230 Because at the very end of this if statement, what we'll do is we'll just return can be crafted. 191 00:13:35,220 --> 00:13:37,800 Otherwise what we could do. 192 00:13:38,580 --> 00:13:44,940 If the player has this material in their player's material folder and they actually have enough amount, 193 00:13:44,970 --> 00:13:51,540 right, they have enough quantity to craft the item, then we'll insert it into the materials to be 194 00:13:51,540 --> 00:13:52,710 deducted table. 195 00:13:52,860 --> 00:13:58,860 So we could do materials to be deducted, pass that material and the amount. 196 00:14:00,260 --> 00:14:02,180 So it'll loop through everything. 197 00:14:02,180 --> 00:14:08,030 So if they want to craft a crystal pickaxe, it'll be like, okay, you need to wood. 198 00:14:08,030 --> 00:14:14,120 So it'll check if the player has a wood material in their players folder and if they have enough quantity, 199 00:14:14,120 --> 00:14:15,230 which would be two. 200 00:14:15,260 --> 00:14:19,190 If they do, then we'll put it in the table and then we'll loop to the next one. 201 00:14:19,190 --> 00:14:20,960 So they need three crystal. 202 00:14:20,960 --> 00:14:27,410 If they only have two, then we'll break out of this loop and we'll have this variable be set to false, 203 00:14:27,410 --> 00:14:29,030 and then we'll return. 204 00:14:29,030 --> 00:14:32,930 We'll tell the player, Hey, you know, you can't craft this thing because you don't have enough materials. 205 00:14:33,980 --> 00:14:38,960 So once we're done looping, it can be crafted is still true. 206 00:14:38,990 --> 00:14:45,260 So if can be crafted, that means they passed everything they have, all the materials they need. 207 00:14:45,290 --> 00:14:49,400 Then what we could do is we could loop through every single material and the materials to be deducted 208 00:14:49,430 --> 00:14:50,030 table. 209 00:14:50,120 --> 00:14:55,790 So for every material and its value in pairs, materials to be deducted. 210 00:14:56,930 --> 00:15:01,730 What we're going to do is we're going to reference their materials folder. 211 00:15:04,580 --> 00:15:06,020 Get that material. 212 00:15:06,680 --> 00:15:12,260 And then we're going to set the value of that material minus equal the value we get here. 213 00:15:12,500 --> 00:15:19,190 So we're deducting whatever material quantity they have to craft this item. 214 00:15:19,460 --> 00:15:25,910 Once we do all of that and we deduct all their materials, then we can clone one of those tools. 215 00:15:25,910 --> 00:15:32,780 And we created a reference earlier to the tools folder and then we can actually get the tool name, 216 00:15:32,780 --> 00:15:38,330 which was the item name passed to us, since the names in here match the tool. 217 00:15:40,190 --> 00:15:41,990 And we can just clone this tool. 218 00:15:42,990 --> 00:15:48,300 And once we have this clone, we can set the parent of the clone equal to the player's backpack. 219 00:15:49,250 --> 00:15:55,280 And then before we return can be crafted, we got to make sure to set craft balance for this player 220 00:15:55,280 --> 00:15:58,250 back to false so they're able to craft again. 221 00:15:58,760 --> 00:15:59,040 All right. 222 00:15:59,060 --> 00:16:00,140 Seems pretty good. 223 00:16:00,290 --> 00:16:05,990 One more thing I want to check here is I want to check if the action the is requesting the server to 224 00:16:05,990 --> 00:16:11,360 do is an action like get craftable items. 225 00:16:11,600 --> 00:16:19,010 And the reason I'm doing this is because I don't want to put a bunch of buttons inside of our item frame 226 00:16:19,010 --> 00:16:21,620 that lists the current items we can craft. 227 00:16:21,620 --> 00:16:24,500 Because let's say down the line I add more and more items to craft. 228 00:16:24,500 --> 00:16:27,260 That means I would have to update the GUI each time. 229 00:16:27,380 --> 00:16:32,000 I would have to update the frames in the GUI, and then I'd also have to update this table. 230 00:16:32,210 --> 00:16:38,810 But it would be better if I could just only update this table and not have to worry about updating the 231 00:16:38,840 --> 00:16:41,270 buttons that are going to be in our item frame. 232 00:16:42,270 --> 00:16:47,670 So what that means is that when a player joins the game, they can request to get all of the craftable 233 00:16:47,670 --> 00:16:52,260 items that exist and we can return that table back to them. 234 00:16:54,540 --> 00:16:58,740 And because we're turning that table back to them, they can loop through every single one of those, 235 00:16:58,770 --> 00:17:03,930 create a bunch of these item buttons, set the name and then the requirements. 236 00:17:03,930 --> 00:17:10,200 And that way we don't have to sit there and create a bunch of buttons inside of our scrolling frame. 237 00:17:10,560 --> 00:17:11,160 But whatever. 238 00:17:11,160 --> 00:17:12,420 You'll see what I mean in a moment. 239 00:17:13,460 --> 00:17:15,800 Otherwise our server script is looking pretty good. 240 00:17:15,830 --> 00:17:21,650 The next thing we need to do is to create a local script inside of our crafting GUI so that way we can 241 00:17:21,650 --> 00:17:24,260 listen for when the player clicks all the buttons in their. 242 00:17:26,630 --> 00:17:29,910 So inside of our guy will, of course, need some services. 243 00:17:29,930 --> 00:17:32,300 We're going to need variables. 244 00:17:32,540 --> 00:17:35,540 We're going to need actually some constants. 245 00:17:36,770 --> 00:17:38,630 As well as functions. 246 00:17:39,620 --> 00:17:41,570 Event handlers. 247 00:17:41,960 --> 00:17:49,310 And then we'll actually have a main section where we're going to invoke the server with the request 248 00:17:49,310 --> 00:17:54,320 craft info, remote function, and we're going to get that table I was talking about earlier so we could 249 00:17:54,320 --> 00:17:55,400 set up our GUI. 250 00:17:56,560 --> 00:18:01,540 So the first service we need, of course, is replicated storage to get access to that event. 251 00:18:05,320 --> 00:18:07,550 We're going to need the player service. 252 00:18:07,570 --> 00:18:10,690 That way we can get a reference to our local player. 253 00:18:12,750 --> 00:18:15,540 And then we're actually going to need the tween service. 254 00:18:15,540 --> 00:18:22,050 That way we can tween elements specifically when we click those buttons, we want to tween the UI onto 255 00:18:22,050 --> 00:18:23,700 the screen and then tween it off the screen. 256 00:18:23,700 --> 00:18:25,020 So it's nice and smooth. 257 00:18:27,950 --> 00:18:31,250 Now that we've got that done, we need some variables. 258 00:18:31,280 --> 00:18:34,070 One is going to be for our event. 259 00:18:35,870 --> 00:18:42,650 And we'll wait for the request craft info to be replicated because this local script is going to run 260 00:18:42,650 --> 00:18:43,580 immediately. 261 00:18:44,540 --> 00:18:46,580 We'll need a reference to the local player. 262 00:18:49,790 --> 00:18:53,780 And we'll need a reference to the guy itself, which is apparent. 263 00:18:54,700 --> 00:18:58,390 And then inside of this guy, I want to reference the item frame. 264 00:19:01,190 --> 00:19:03,800 And I also want to reference the material frame. 265 00:19:07,600 --> 00:19:11,270 Um, I'll also want to reference those two examples. 266 00:19:11,270 --> 00:19:16,630 So I made an example item button and the item frame, and then I made an example material frame inside 267 00:19:16,630 --> 00:19:17,560 of the materials frame. 268 00:19:17,560 --> 00:19:20,650 So I want to reference those two so I can clone them later. 269 00:19:21,820 --> 00:19:30,610 So example item button is equal to the item frame and we'll wait for the example item button to replicate. 270 00:19:31,300 --> 00:19:34,960 And then we'll do the same, for example, material frame. 271 00:19:35,760 --> 00:19:37,890 Material frame wafer child. 272 00:19:37,920 --> 00:19:39,960 The example material frame. 273 00:19:41,570 --> 00:19:43,790 Now, what are these constants I need? 274 00:19:43,880 --> 00:19:50,270 Well, one of the constants I'll need is a string, and it's going to be called quantity. 275 00:19:50,750 --> 00:19:51,620 String. 276 00:19:52,070 --> 00:19:53,180 What is this going to be? 277 00:19:53,180 --> 00:19:55,520 It's just going to be a string that says quantity. 278 00:19:56,730 --> 00:20:03,140 Because I'm going to be using the string to concatenate the quantity of the materials a player has. 279 00:20:03,150 --> 00:20:08,130 And I don't want to have to copy and paste the same string in multiple spots that just wastes more memory. 280 00:20:08,130 --> 00:20:13,110 Instead, I can just store it in one constant and then reference that constant so I save on memory. 281 00:20:13,980 --> 00:20:22,590 Another constant I'll need is the position for our item frame when it's visible and when it's not visible. 282 00:20:22,590 --> 00:20:29,220 So we can call this first one item frame hidden and it's going to be equal to a new item to. 283 00:20:30,330 --> 00:20:37,650 So when our item frame is hidden right here on the right side of the screen, it's currently at an X 284 00:20:37,650 --> 00:20:39,240 scale position of one. 285 00:20:39,780 --> 00:20:43,140 So for the first number in here, it's got to be the X scale position. 286 00:20:43,140 --> 00:20:44,670 So that's going to be one. 287 00:20:44,850 --> 00:20:50,610 The offset is zero and then the Y scale is going to be that particular item frame. 288 00:20:50,610 --> 00:20:54,870 So item frame dot position, we get the Y and then we get the scale. 289 00:20:54,900 --> 00:20:56,400 The offset is zero. 290 00:20:57,130 --> 00:20:58,720 Then we can copy this. 291 00:20:59,790 --> 00:21:02,070 And do it for when the frame is visible. 292 00:21:02,070 --> 00:21:09,660 And if you remember, the frame is visible when we are at a scale position on the x axis of 75%. 293 00:21:11,380 --> 00:21:17,410 Keep the same Y scale and then we could just copy these two and do the same for the material frame. 294 00:21:17,410 --> 00:21:19,090 So material frame. 295 00:21:25,290 --> 00:21:29,820 When the material frame is hidden, our X scale is going to actually be at zero. 296 00:21:29,820 --> 00:21:37,260 And when it's visible, our X scale is going to be at 0.25 and then we'll make sure to get the material 297 00:21:37,260 --> 00:21:41,730 frame position on the Y axis. 298 00:21:43,450 --> 00:21:43,780 All right. 299 00:21:43,780 --> 00:21:44,920 That seems pretty good. 300 00:21:45,130 --> 00:21:47,230 Now, some functions we're going to need. 301 00:21:47,620 --> 00:21:52,930 One function is going to be for when we click one of these item buttons. 302 00:21:52,930 --> 00:21:56,500 So we're going to have a list of four pickaxes we can craft. 303 00:21:56,620 --> 00:22:00,730 Now, I don't want to create a lambda function for each one of those buttons because that just wastes 304 00:22:00,730 --> 00:22:01,320 memory. 305 00:22:01,330 --> 00:22:05,080 Instead, I can have each button reference the same function. 306 00:22:05,080 --> 00:22:07,450 So in particular, it's going to be this function. 307 00:22:07,450 --> 00:22:10,660 I'm going to name it on item button clicked. 308 00:22:11,020 --> 00:22:14,350 We're going to pass the button that got clicked to this function. 309 00:22:14,530 --> 00:22:17,620 And what we'll do is we'll have that request craft event. 310 00:22:17,620 --> 00:22:19,510 We're going to invoke the server. 311 00:22:21,560 --> 00:22:28,610 And we're going to tell the server we want to craft an item and that item is going to be the button 312 00:22:28,640 --> 00:22:32,690 dot item name, label, dot text. 313 00:22:33,140 --> 00:22:39,440 So inside of this button I have a text label that's going to represent the name of the tool we can craft. 314 00:22:39,440 --> 00:22:43,730 So one will be wooden pickaxe, one will be crystal Pickaxe and so on. 315 00:22:43,730 --> 00:22:47,120 So we're telling the server, Hey, we want to craft an item. 316 00:22:47,120 --> 00:22:48,380 Which item is it? 317 00:22:48,410 --> 00:22:50,480 It's the text that is in the button. 318 00:22:50,480 --> 00:22:51,260 We clicked. 319 00:22:52,050 --> 00:22:54,540 And then we can actually store the result from this. 320 00:22:56,300 --> 00:22:58,910 And then we can print that results. 321 00:23:00,110 --> 00:23:01,040 In the console. 322 00:23:02,190 --> 00:23:10,860 Next, we'll need a function for listening when those int values that are inside of our materials folder 323 00:23:10,890 --> 00:23:12,120 get updated. 324 00:23:12,330 --> 00:23:17,400 So let's say a value gets deducted or a value gets added to that int value. 325 00:23:17,400 --> 00:23:25,350 I want to be able to update those numbers in our guy so we can call this function on int value updated. 326 00:23:25,800 --> 00:23:33,390 We'll pass that int value to this function itself and then we'll also pass the label that's going to 327 00:23:33,390 --> 00:23:34,300 update the quantity. 328 00:23:34,320 --> 00:23:38,610 So we're going to call it quantity label and that's what we're going to be using this quantity string 329 00:23:38,610 --> 00:23:39,150 for. 330 00:23:39,990 --> 00:23:46,440 So inside of our example material frame, there is a quantity label here and it's going to display the 331 00:23:46,440 --> 00:23:49,680 quantity of items for that particular material. 332 00:23:50,100 --> 00:23:56,490 So what we can do is we can get that quantity label and set the text equal to the quantity string, 333 00:23:56,490 --> 00:24:00,510 and then we concatenate it with the int values new value. 334 00:24:00,690 --> 00:24:07,140 So when the int values value changes, we display that new value inside of our quantity label. 335 00:24:07,710 --> 00:24:09,270 Another function we'll need. 336 00:24:10,310 --> 00:24:15,530 Is for those open and closing buttons for each one of those item frames. 337 00:24:16,110 --> 00:24:21,270 So we can call this function on open close button clicked. 338 00:24:22,820 --> 00:24:29,480 And since this function needs to work for each one of those buttons, what I'll actually do is we'll 339 00:24:29,480 --> 00:24:32,300 pass what frame we need to tween. 340 00:24:34,470 --> 00:24:40,980 And then we'll pass the UDM position we want to tween to, which is why we created these constants. 341 00:24:40,980 --> 00:24:47,220 So we're going to call this function and pass those constants so we can, you know, tween the frame 342 00:24:47,220 --> 00:24:48,090 left and right. 343 00:24:48,420 --> 00:24:52,140 And what we'll do is we'll reference the tween service and we're going to create a new tween. 344 00:24:53,190 --> 00:24:56,610 The tween is going to be on that frame, the tween. 345 00:24:57,410 --> 00:24:59,240 We'll create a new tween info. 346 00:25:00,830 --> 00:25:03,620 And it'll just be like, one second, let's just do that. 347 00:25:04,010 --> 00:25:09,410 And then what do we need to tween We need to tween the position of our UI element, which is going to 348 00:25:09,410 --> 00:25:12,500 be equal to that udim position that gets passed to this function. 349 00:25:12,500 --> 00:25:14,120 And then we just play the tween. 350 00:25:15,120 --> 00:25:17,400 Think that's all we need this function to do. 351 00:25:17,640 --> 00:25:21,390 Now that we've got all that done, I think that's actually all the functions we'll need. 352 00:25:21,420 --> 00:25:24,270 Now we need to handle some events. 353 00:25:24,300 --> 00:25:25,770 What events we need to handle. 354 00:25:25,800 --> 00:25:31,920 Well, the first events we can handle are the open and close buttons for our crafting GUI. 355 00:25:32,520 --> 00:25:34,950 So we could do material frame. 356 00:25:36,090 --> 00:25:39,480 Um, we'll wait for the open button. 357 00:25:40,950 --> 00:25:45,660 And when that button gets clicked, we're going to connect a lambda function to it. 358 00:25:46,240 --> 00:25:53,980 And we're going to call the on open close button clicked past the material frame. 359 00:25:55,560 --> 00:26:03,030 And then we're going to pass material frame visible because this is the open button we want to tween 360 00:26:03,030 --> 00:26:04,980 the material frame to be visible. 361 00:26:05,310 --> 00:26:15,390 Once we do that, what I want to do is in the material frame, I want to set the open button to be no 362 00:26:15,390 --> 00:26:17,790 longer visible because we just opened it. 363 00:26:18,330 --> 00:26:23,940 And then I also want to set the close button to be visible so that way we can close the GUI. 364 00:26:25,810 --> 00:26:30,610 And then we can actually just copy this and do the same thing for the close button. 365 00:26:31,460 --> 00:26:33,770 And instead we want the frame to be hidden. 366 00:26:34,100 --> 00:26:37,550 And we can just reverse these booleans here. 367 00:26:38,700 --> 00:26:42,740 So we want the open button to be visible and we want the close button to not be visible. 368 00:26:42,750 --> 00:26:50,220 And we can just copy these again and do the same thing for the open and close buttons on the item frame. 369 00:26:51,850 --> 00:26:56,140 There is a more efficient way to do this, but this is probably the easiest way. 370 00:26:57,460 --> 00:27:01,600 And then we want to pass the item frame itself to this function. 371 00:27:03,270 --> 00:27:05,870 And we want to reference that here as well. 372 00:27:05,880 --> 00:27:07,530 So change these references. 373 00:27:09,490 --> 00:27:14,140 And then we need to change this to item frame instead of material frame. 374 00:27:15,620 --> 00:27:16,640 Just like that. 375 00:27:17,570 --> 00:27:17,960 All right. 376 00:27:17,960 --> 00:27:21,470 So that's all the event handlers we need to listen to for this section. 377 00:27:21,560 --> 00:27:25,040 Now, what we're going to do is I'm going to create a variable. 378 00:27:25,040 --> 00:27:30,350 I'm going to call it craftable items, and it's going to be equal to the request craft event. 379 00:27:30,350 --> 00:27:35,660 We're going to invoke the server and we're going to get all the current craftable items. 380 00:27:35,660 --> 00:27:40,580 So we're going to get that table that's going to be returned from us from the server, this table right 381 00:27:40,580 --> 00:27:41,090 here. 382 00:27:42,360 --> 00:27:46,800 Once we've grabbed this table, what we could do now is we can loop through it. 383 00:27:46,800 --> 00:27:55,020 So for every single item and its requirements inside of this table, craftable items, what we're going 384 00:27:55,020 --> 00:28:00,120 to do is we're going to create a bunch of buttons for each of the items. 385 00:28:00,120 --> 00:28:05,970 So I'm going to create a clone and this clone is going to be a clone of the example item button. 386 00:28:08,160 --> 00:28:09,120 For this clone. 387 00:28:09,120 --> 00:28:13,260 I'm going to set the item name, label the name of our item. 388 00:28:13,260 --> 00:28:16,770 We need to craft set the text equal to that item. 389 00:28:18,500 --> 00:28:26,060 And then what I'm going to do next is for every single material and the amount required inside of the 390 00:28:26,060 --> 00:28:29,150 requirements table for this particular item. 391 00:28:30,080 --> 00:28:36,080 So for every single material and amount for this particular requirements for the item, we're going 392 00:28:36,080 --> 00:28:37,460 to set the clone. 393 00:28:38,940 --> 00:28:45,420 And get the requirements label and we're going to set the text equal to itself. 394 00:28:45,420 --> 00:28:47,300 So we're actually going to copy this. 395 00:28:47,310 --> 00:28:54,270 We're going to set this text to itself and then we're going to concatenate it with the amount. 396 00:28:55,040 --> 00:29:03,470 Concatenate that amount with a space and then concatenate it with the name of the material and then 397 00:29:03,470 --> 00:29:05,990 concatenate it with a comma. 398 00:29:07,090 --> 00:29:08,620 And a space. 399 00:29:08,830 --> 00:29:14,500 So through the first iteration of the loop, let's say it'll say something like requires three would 400 00:29:14,500 --> 00:29:17,950 and then comma and then it adds on another amount. 401 00:29:17,950 --> 00:29:20,020 So this amount will be maybe. 402 00:29:21,020 --> 00:29:26,180 Uh, two crystal comma, and then it goes through all the amounts and sets the requirements label to 403 00:29:26,210 --> 00:29:27,140 that text. 404 00:29:28,470 --> 00:29:33,900 Once we've got this done for this particular clone, this particular example item button, we need to 405 00:29:33,900 --> 00:29:35,460 listen for when it gets clicked. 406 00:29:35,460 --> 00:29:39,360 So mouse button one click, we're going to connect a lambda function. 407 00:29:39,540 --> 00:29:46,380 And the reason I'm not referencing our on item button clicked is because I actually have to pass the 408 00:29:46,380 --> 00:29:48,180 button itself for this function to work. 409 00:29:48,180 --> 00:29:53,520 So I need to pass the button and in order to do that I have to create a lambda function here. 410 00:29:53,520 --> 00:30:00,420 So inside I can call the on item button clicked and then pass the clone to this function. 411 00:30:00,660 --> 00:30:07,830 Once that's done, we can set the parent of the clone equal to the item frame and we're going to wait 412 00:30:07,830 --> 00:30:11,370 for the scrolling frame just in case it hasn't replicated yet. 413 00:30:11,850 --> 00:30:15,150 And then of course, we need to set the visibility of this button to true. 414 00:30:16,480 --> 00:30:21,790 Now, the next thing we need to do is we are going to have to loop through every single one of those 415 00:30:21,820 --> 00:30:24,520 int values that are a part of our materials folder. 416 00:30:24,550 --> 00:30:28,810 That way we can now set up the materials frame in our guy. 417 00:30:28,840 --> 00:30:35,770 So to do that we're going to get every single int value and I'm going to make sure to denote this as 418 00:30:35,770 --> 00:30:41,050 an int value and I pairs, we're going to get the local player. 419 00:30:41,440 --> 00:30:47,830 We're going to wait for the materials folder in case it hasn't replicated yet and we're going to get 420 00:30:47,830 --> 00:30:50,980 the children inside of this folder. 421 00:30:51,700 --> 00:30:56,770 So for every single one of these int values, we're going to create a clone of the example material 422 00:30:56,770 --> 00:30:57,490 frame. 423 00:31:00,000 --> 00:31:08,970 The material name label for the clone is going to be equal to the int value.name the name of the int 424 00:31:08,970 --> 00:31:09,630 value. 425 00:31:10,980 --> 00:31:19,050 The clone quantity label text, of course, is going to be equal to quantity string and concatenate 426 00:31:19,050 --> 00:31:21,360 it with the int value. 427 00:31:21,390 --> 00:31:22,950 Dot value. 428 00:31:24,480 --> 00:31:28,170 And then we can do something like int value. 429 00:31:28,200 --> 00:31:31,080 We're going to get property change signal. 430 00:31:31,080 --> 00:31:33,480 What property do we want to listen to for when it changes? 431 00:31:33,480 --> 00:31:36,000 Well, we want to listen to when the value changes. 432 00:31:36,580 --> 00:31:39,650 And when the value changes, what do we want to do? 433 00:31:39,670 --> 00:31:43,570 Well, we want to call that on int value updated function. 434 00:31:43,570 --> 00:31:50,590 We want to pass this particular int value to the function as well as the quantity label that is a part 435 00:31:50,590 --> 00:31:53,680 of this particular material frame clone. 436 00:31:54,070 --> 00:31:59,980 So that way this function will update the text in this quantity label to the correct value that is in 437 00:31:59,980 --> 00:32:00,900 this int value. 438 00:32:00,910 --> 00:32:06,220 And that happens every time the value property gets updated or changed. 439 00:32:06,820 --> 00:32:13,150 And again, we need to set the parent of this clone to be equal to the material frame. 440 00:32:13,150 --> 00:32:17,830 And we're going to wait for the scrolling frame in case it hasn't been replicated. 441 00:32:19,090 --> 00:32:23,920 And then we're going to set the visibility of the clone to true as well. 442 00:32:25,130 --> 00:32:32,930 Now, one thing I'm going to do just in case is I'm going to listen for when any children gets added 443 00:32:32,930 --> 00:32:34,760 to our materials folder. 444 00:32:34,790 --> 00:32:36,590 So let's say down the line. 445 00:32:37,280 --> 00:32:40,650 Maybe the player gets a new material later in the game. 446 00:32:40,670 --> 00:32:44,270 Well, I want to make sure to be able to update that in my guy. 447 00:32:44,510 --> 00:32:47,740 So what we could do is we can access the materials folder. 448 00:32:47,750 --> 00:32:51,590 I don't have to wait for it here because I already waited for it up here. 449 00:32:51,590 --> 00:32:53,060 I don't need to wait for it again. 450 00:32:53,900 --> 00:32:56,930 And we're going to listen to the child added event. 451 00:32:58,480 --> 00:33:05,620 And we're going to get that child that gets added to this folder and we're going to check if this child 452 00:33:05,620 --> 00:33:09,570 is not an int value, then we don't care. 453 00:33:09,580 --> 00:33:11,140 We're going to return. 454 00:33:12,910 --> 00:33:19,870 Otherwise, if it isn't in value, then we're going to make another clone of the example material frame. 455 00:33:21,230 --> 00:33:24,560 And technically we can actually just copy the same thing here. 456 00:33:27,020 --> 00:33:32,120 Um, we'll just want to change this to child. 457 00:33:34,380 --> 00:33:36,510 For each one of these references. 458 00:33:39,000 --> 00:33:43,980 And other than that, I think I think we're pretty good for our local script. 459 00:33:44,130 --> 00:33:50,010 So this should be able to handle all of the opening and closing buttons and it should be able to handle 460 00:33:50,040 --> 00:33:53,730 all of the buttons that are going to be inside of our item frame. 461 00:33:53,730 --> 00:33:59,250 And it should also be able to update every single one of the materials inside of the materials frame 462 00:33:59,250 --> 00:34:06,030 for our materials folder so we can actually go ahead and test it out by playing our game. 463 00:34:06,360 --> 00:34:12,510 So when we play our game, we should be able to open and close these menus and there we are. 464 00:34:13,160 --> 00:34:13,820 Perfect. 465 00:34:13,820 --> 00:34:20,750 So as you can see, our loop went through and updated each of the materials and the crafting items that 466 00:34:20,750 --> 00:34:23,750 we can craft, and it's put down the requirements. 467 00:34:23,750 --> 00:34:28,440 So the wooden pickaxe is five wood, crystal, pickaxe, two wood, three crystal, blah, blah, blah. 468 00:34:28,460 --> 00:34:34,550 And it's telling us our current quantity for these materials and that's actually stored in this material 469 00:34:34,550 --> 00:34:36,410 folders that are part of our player. 470 00:34:36,440 --> 00:34:40,100 You can see each one of these int values and their value is at zero. 471 00:34:40,790 --> 00:34:44,900 Now if I were to go and look on the server. 472 00:34:45,830 --> 00:34:50,840 And let's say I just updated these values to like ten for each one of them. 473 00:34:55,500 --> 00:34:57,060 And I go back to the client. 474 00:34:57,090 --> 00:35:02,370 The client's GUI should be updated to show each one of these materials set to ten because the value 475 00:35:02,370 --> 00:35:04,830 got changed and it should have fired that event. 476 00:35:04,980 --> 00:35:06,360 And look, there we are. 477 00:35:06,390 --> 00:35:10,020 Each one of the quantities for our materials got set to ten. 478 00:35:10,610 --> 00:35:14,570 And that means we should be able to craft any of these items here that we want. 479 00:35:14,600 --> 00:35:17,270 So let's go ahead and craft maybe a wooden pickaxe. 480 00:35:18,090 --> 00:35:18,890 There we go. 481 00:35:18,900 --> 00:35:20,490 We get our wooden pickaxe. 482 00:35:20,490 --> 00:35:26,700 And because we had that print statement in our local script, it's telling us, hey, our our request 483 00:35:26,700 --> 00:35:30,810 to craft an item was successful and it also detected us five wood. 484 00:35:31,020 --> 00:35:37,080 We could go ahead and try to craft a crystal pickaxe again, took the wood away and it also took the 485 00:35:37,080 --> 00:35:37,830 crystal away. 486 00:35:37,830 --> 00:35:39,720 And now we have our crystal pickaxe. 487 00:35:40,380 --> 00:35:42,420 We can do it for the iron pickaxe. 488 00:35:42,450 --> 00:35:43,470 There we go. 489 00:35:43,710 --> 00:35:48,480 And then if I try to craft the stone pickaxe, it shouldn't let me because I don't have enough wood. 490 00:35:49,130 --> 00:35:49,520 There we go. 491 00:35:49,520 --> 00:35:50,480 We got false. 492 00:35:52,400 --> 00:35:55,490 And of course, in your game you could set it up too. 493 00:35:55,520 --> 00:36:00,590 However, you'd like the player to get more of these materials and these tools don't actually do anything. 494 00:36:00,590 --> 00:36:01,820 They're just models. 495 00:36:01,820 --> 00:36:06,140 But you could also create these tools to do whatever you want for your game. 496 00:36:06,140 --> 00:36:11,480 So maybe the player is in like a mining game and they've got to go mine for materials to get better 497 00:36:11,480 --> 00:36:13,940 pickaxes or whatever the case may be. 498 00:36:14,580 --> 00:36:21,150 But otherwise, this is a very simple way of how to create a crafting system in Roblox. 499 00:36:21,180 --> 00:36:27,330 Obviously you could go absolutely crazy with crafting systems and make them as complex and crazy as 500 00:36:27,330 --> 00:36:32,190 you'd want, but this lecture should be able to demonstrate to you a basic crafting system that should 501 00:36:32,190 --> 00:36:35,610 get you off of your feet and into more scripting. 502 00:36:35,700 --> 00:36:37,710 So I hope you enjoyed this lecture.